/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.util.common;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPdfExporter;

/**
 *
 * @author Administrator
 */
public class PrintToolKit {

    public static void print(Map map, String jasperFile) throws Exception {
        //Map map = reportParameter();//parameters
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.responseComplete();

        ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
        //JasperPrint jasperPrint = JasperFillManager.fillReport(scontext.getRealPath(jasperFile), map, JdbcOperation.dbConn());
        JasperPrint jasperPrint = JasperFillManager.fillReport(scontext.getRealPath(jasperFile), map);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();
        byte[] bytes = baos.toByteArray();

        if (bytes != null && bytes.length > 0) {
            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "inline; filename=\"relatorioPorData.pdf\"");
            response.setContentLength(bytes.length);
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(bytes, 0, bytes.length);
            outputStream.flush();
            outputStream.close();
        }
    }

    public static void print(Map map, String... jasperFile) throws Exception {

        ArrayList jasperPrintList = new ArrayList();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.responseComplete();
        ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();

        for (String path : jasperFile) {
            JasperPrint jasperPrint = JasperFillManager.fillReport(scontext.getRealPath(path), map);
            jasperPrintList.add(jasperPrint);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRPdfExporter exporter = new JRPdfExporter();
        //exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();

        byte[] bytes = baos.toByteArray();

        if (bytes != null && bytes.length > 0) {
            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "inline; filename=\"relatorioPorData.pdf\"");
            response.setContentLength(bytes.length);
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(bytes, 0, bytes.length);
            outputStream.flush();
            outputStream.close();
        }
    }

    public static void printBills(Map map, String jasperFile, String jasperFileTow) throws Exception {
        //Map map = reportParameter();//parameters
        ArrayList jasperPrintList = new ArrayList();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        facesContext.responseComplete();

        ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
        //JasperPrint jasperPrint = JasperFillManager.fillReport(scontext.getRealPath(jasperFile), map, JdbcOperation.dbConn());
        JasperPrint jasperPrint = JasperFillManager.fillReport(scontext.getRealPath(jasperFile), map);
        jasperPrintList.add(jasperPrint);
        JasperPrint jasperPrintBill = JasperFillManager.fillReport(scontext.getRealPath(jasperFileTow), map);
        jasperPrintList.add(jasperPrintBill);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRPdfExporter exporter = new JRPdfExporter();
        //exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();
        byte[] bytes = baos.toByteArray();

        if (bytes != null && bytes.length > 0) {
            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "inline; filename=\"relatorioPorData.pdf\"");
            response.setContentLength(bytes.length);
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(bytes, 0, bytes.length);
            outputStream.flush();
            outputStream.close();
        }
    }
}
